<?php

/**
 * entry actions.
 *
 * @package    cms
 * @subpackage entry
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class entryActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {       
    $this->pager = Cms::setPagination($request);  
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->cms_entry = Doctrine_Core::getTable('cmsEntry')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->cms_entry);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new cmsEntryForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new cmsEntryForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($cms_entry = Doctrine_Core::getTable('cmsEntry')->find(array($request->getParameter('id'))), sprintf('Object cms_entry does not exist (%s).', $request->getParameter('id')));
    $this->form = new cmsEntryForm($cms_entry);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($cms_entry = Doctrine_Core::getTable('cmsEntry')->find(array($request->getParameter('id'))), sprintf('Object cms_entry does not exist (%s).', $request->getParameter('id')));
    $this->form = new cmsEntryForm($cms_entry);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($cms_entry = Doctrine_Core::getTable('cmsEntry')->find(array($request->getParameter('id'))), sprintf('Object cms_entry does not exist (%s).', $request->getParameter('id')));
    $cms_entry->delete();

    $this->redirect('entry/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $cms_entry = $form->save();

      $this->redirect('entry/edit?id='.$cms_entry->getId());
    }
  }   
  
  public function executeSearch(sfWebRequest $request)
  {
    $this->forwardUnless($query = $request->getParameter('query'), 'entry', 'index');

    $this->pager = Cms::setPagination($request, Doctrine_Core::getTable('CmsEntry') ->getForLuceneQuery($query));
  }
}